// 2_4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include<string>
#include<vector>
#include<list>
using namespace std;

////动态增长字符数组
//template<class T>
//class basic_string
//{
//
//private:
//	T* _str;
//	size_t _size;
//	size_t _capacity;
//};
//重载[]
//char& operator[](size_t i)
// 
//{
// assert(i<_size)
//	return _str[i];
//}

void String_test2()
{
	string str1 = "1234";

	//第一种方式   下标[]   因为重载了[]
	for (size_t i = 0; i < str1.size(); i++)
	{
		str1[i]++;
	}
	cout << str1 << endl;

	//第二种方式
	for (auto& ch : str1)
	{
		ch--;
	}
	cout << str1 << endl;


	//逆置字符串

	size_t begin = 0, end = str1.size() - 1;
	while (begin < end)
	{
		swap(str1[begin++], str1[end--]);
	}
	cout << str1 << endl;

	//迭代器
	for (string::iterator it = str1.begin(); it != str1.end(); it++)
	{
		*it+=1;
	}
	cout << str1 << endl;


	//反转
	//reverse(str1.begin(), str1.end());
	//cout << str1 << endl;
}


//String的迭代器  
//string::iterator it = str.begin(); 这里的迭代器有种类似指针的用法
void tets_const_string(const string& s)  //进行权限的缩小，只能string::const_iterator用来接受
{
	//string::iterator it = s.begin();//会报错，这里进行了权限的放大
	string::const_iterator cit = s.begin();  //注意这里的const 修饰的是cit这个变量指向的内容，即*cit，而不是cit
											//如果写成const string::iterator it = s.begin();修饰的是it
	//第二种写法 
	auto cit2 = s.begin();
	cout << "string::const_iterator" << endl;
	while (cit != s.end())
	{
		cout << *cit << " ";
		cit++;
	}
	cout << endl;

	//对于逆置访问跟上诉访问是一样的
	string::const_reverse_iterator crit = s.rbegin();
	cout << "string::const_reverse_iterator" << endl;
	while (crit != s.rend())
	{
		cout << *crit << " ";
		crit++;
	}
	cout << endl;
}

void String_test3()
{
	string str = "1234";
	//有两种类型  一种是非const类型，一种是const类型

	string::iterator it = str.begin();
	while (it != str.end())
	{
		*it += 1;
		cout << *it << " ";
		it++;
	}
	cout <<  endl;
	//倒着访问
	string::reverse_iterator rit = str.rbegin();
	while (rit != str.rend())
	{
		*rit+=1;
		cout << *rit << " ";
		rit++;
	}
	cout <<  endl;

	//测试const_iterator  只遍历，不修改值
	tets_const_string(str);

	vector<int> vec;
	vector<int>::iterator vit = vec.begin();
	while (vit != vec.end())
	{
		*vit += 1;
		cout << *vit << " ";
		vit++;
	}


}


//string的一些capcity函数
void String_test4()
{
	string str = "hello world";
	cout << str.size() << endl;         //只对所有的数据结构
	cout << str.length() << endl;       //对于vector ，list等一些适用，树，图就不适用了
	cout << str.capacity() << endl;
	cout << "max_size:" << str.max_size() << endl;
	//clear()函数，用来清除数据，但是size大小不变
	cout << endl << endl;
	str.clear();                        //将有效字符串清楚掉，保留空间大小
	cout << str.size() << endl;        
	cout << str.length() << endl;       
	cout << str.capacity() << endl;
	cout << endl << endl;


	//empty函数，判断是否是空字符串
	cout << endl << endl;
	cout << str.empty() << endl;
	str += "hello wrold";
	cout << str.empty() << endl;                        
}

//resize()和reserve
void String_test5()
{
	//resize函数主要是对已有的字符串进行调整，分为3种情况
	//1、第一种情况  当resize的n<size()的时候
	string str = "hello world";
	str.resize(5);
	cout << str.length() << endl;
	cout << str.capacity() << endl;
	cout << str << endl<<endl;

	//2、当    size<n<=capacity 时，会尾插值
	str += " world";
	str.resize(15, 'x');
	cout << str.length() << endl;
	cout << str.capacity() << endl;
	cout << str << endl<<endl;

	str.resize(11);
	//3、当n>capacity时，会进行扩容+尾插
	str.resize(20, 'x');
	cout << str.length() << endl;
	cout << str.capacity() << endl;
	cout << str << endl;

}
void String_test6()
{
	string str = "hello world";
	cout << str.capacity() << endl;
	cout << "enlarge storge::" << endl;
	for (size_t i = 0; i < 1000; i++)
	{
		str.push_back('c');
		if (str.size() == str.capacity())
		{
			cout << str.capacity() << endl;
		}
	}
	cout << str.capacity() << endl;

}
void String_test7()
{
	string str = "hello world";
	cout << str.length() << endl;
	cout << str.capacity() << endl;
	str.reserve(20);
	cout << str.length() << endl;
	cout << str.capacity() << endl;

	str.shrink_to_fit();
	cout << str.length() << endl;
	cout << str.capacity() << endl;
}


int main()
{

	String_test7();
	//string str1("banana");


	return 0;
}





